package offer;

import java.util.ArrayList;
import java.util.List;

public class Solution34 {
    List<List<Integer>> res;

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        res = new ArrayList<>();
        pathSum(new ArrayList<>(), root, targetSum);
        return res;
    }

    public void pathSum(List<Integer> list, TreeNode root, int target) {
        if (root == null) {
            return;
        }
        list.add(root.val);
        target -= root.val;
        if (target == 0 && root.left == null && root.right == null) {
            res.add(new ArrayList<>(list));
        }
        pathSum(list, root.left, target);
        pathSum(list, root.right, target);
        list.remove(list.size() - 1);
    }
}
